package com.ysx.job;
import com.constant.BusinessContants;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
 * @Classname DelayInvoiceHandler
 * @Description TODO
 * @Date 2021/11/21 18:04
 * @Author yyy
 */
@Component
public class DelayInvoiceHandler {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    private static Logger logger = Logger.getLogger(DelayInvoiceHandler.class);

    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    //记录暂缓时间
    public void insertDelayInvoiceStatus() throws ParseException {
        UUID uuid = UUID.randomUUID();
        System.out.println("uuid1 = " + uuid);
        //获取一个日历类
        Date date = new Date();
        Calendar calendar=Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.MONTH,6);//过期时间设置为半年后
        //过期时间为半年
        Date delayInvoiceDate = calendar.getTime();
        calendar.add(Calendar.DATE,7);
        long expireDateMillis = calendar.getTimeInMillis();
        long day= (expireDateMillis-date.getTime()) / (1000 * 60 * 60 * 24);

        long time = delayInvoiceDate.getTime();

        stringRedisTemplate.opsForValue().set(BusinessContants.ysx.FMS_DELAY_INVOICE+uuid.toString(),String.valueOf(time),day, TimeUnit.DAYS);
    }

    //获取暂缓时间并更新
    public void updateDelayInvoiceStatus() throws ParseException {

        // *号 必须要加，否则无法模糊查询
        String prefix = BusinessContants.ysx.FMS_DELAY_INVOICE+"*";
        // 获取所有的key
        Set<String> keys = stringRedisTemplate.keys(prefix);// 批量获取所有的key
//非空判断
        if (CollectionUtils.isEmpty(keys)) {
            System.out.println("暂无暂缓开票数据!");
        } else {

            //遍历所有的Key 去除前缀 获取到Uuid 根据uuid获取到所有的数据以及对应的日期 判断当前时间是否超过redis中的过期时间
            keys.forEach(prefixUuid->{
                String expire = stringRedisTemplate.opsForValue().get(prefixUuid);
                Date expireDate=null;
                if (StringUtils.isEmpty(expire)) {
                    System.out.println("过期时间为空!");
                } else {
                    try {
                        expireDate = sdf.parse(expire);
                        //判断当前时间是否在过期时间之后
                        if (new Date().after(expireDate)) {
                            String uuid = prefixUuid.replace(BusinessContants.ysx.FMS_DELAY_INVOICE, "");
                            System.out.println("订单更新暂缓状态!");
                            //记录操作日志
                        }
                    } catch (ParseException e) {
                        System.out.println("订单的日期格式错误");
                    }
                }
            });

        }


    }



}
